<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermissionFn(1, '0')">添加权限</el-button>
        </div>

        <!-- 注意这里是table数据渲染,data和prop -->
        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <!-- v-if判断,父级有添加功能,自己没有添加功能 -->
              <el-button v-if="scope.row.pid === '0'" type="text" @click="addPermissionFn(2, scope.row.id)">
                添加
              </el-button>
              <el-button type="text" @click="edit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="delFn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加职能--弹框 -->
      <per-dialog
        ref="perDialog"
        :show-dialog.sync="showPerDialog"
        :click-id="clickId"
        :is-edit="isEdit"
        :origin-list="perList"
        @confirmEV="confirmFn"
      />
    </div>
  </div>
</template>

<script>
import {
  getPermissionListAPI,
  addPermissionAPI,
  getPermissionDetailAPI,
  updatePermissionAPI,
  delPermissionAPI
} from '@/api'
import { transTree } from '@/utils/index'
import perDialog from './component/perDialog.vue'
export default {
  components: {
    perDialog
  },
  data() {
    return {
      permissionList: [], // 权限点列表(树形结构)
      perList: [], // 权限点列表(扁平结构)
      type: 0,
      pid: '',
      showPerDialog: false,
      isEdit: false,
      clickId: ''
    }
  },
  created() {
    this.getPermissionListFn()
  },
  methods: {
    // 获取权限列表
    async getPermissionListFn() {
      const res = await getPermissionListAPI()
      this.perList = res.data
      this.permissionList = transTree(res.data, '0')
    },
    // 添加功能-->点击事件
    addPermissionFn(type, pid) {
      this.isEdit = false
      this.showPerDialog = true
      this.type = type
      this.pid = pid
      // pid为'0'/点击那行的id值
      // 添加一级/二级
      this.clickId = pid
    },
    // 添加权限-->接口调用(弹框点确定了)
    async confirmFn(dateObj) {
      if (this.isEdit) {
        // 编辑状态->调用接口
        // 编辑状态返回的有pid和type,所以不需要再做处理
        const res = await updatePermissionAPI(dateObj)
        console.log(res)
      } else {
        /*
      {
        ...dateObj,
        type = this.type,
        pid = this.pid
      }
      */
        dateObj.type = this.type
        dateObj.pid = this.pid
        const res = await addPermissionAPI(dateObj)
        console.log(res)
      }
      // 请求权限列表数据->刷新数据
      this.getPermissionListFn()
    },
    // 编辑的弹框
    async edit(id) {
      this.isEdit = true
      // 获取当前点击项的id值
      this.clickId = id
      this.showPerDialog = true
      const res = await getPermissionDetailAPI(id)
      // console.log(res)
      // 数据回填,利用组件标签上的ref选择到子组件上的formData(非主流做法)
      // md上的做法:利用传值props接收数据,再用watch监听formData再赋值
      this.$refs.perDialog.formData = res.data
    },
    // 删除功能
    async delFn(id) {
      await delPermissionAPI(id)
      this.getPermissionListFn()
    }
  }
}
</script>

<style lang="scss" scoped></style>
